import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index';
import {removeSessionStorage} from '@/common/utils';

Vue.use(VueRouter)
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [{
    path: '/',
    name: 'index',
    meta: {
        needLogin: true
    },
    component: () =>
        import('../views/Index')
},
{
    path: '/login',
    name: 'login',
    component: () =>
        import('@/views/Index')
},
{
    path: '/home',
    name: 'home',
    component: () =>
        import('@/views/Home'),
    children: [
        {
            path: 'main',
            name: 'main',
            component: () => import('@/components/Main'),


        },
        {
            path: 'personal',
            name: 'personal',
            redirect: 'personal/active',
            component: () => import('@/views/person/Personal'),
            children: [
                {
                    path: 'active',
                    name: 'active',
                    component: () => import('@/components/person/Active')
                },
                {
                    path: 'answer',
                    name: 'answer',
                    component: () => import('@/components/person/Answer')
                },
                {
                    path: 'collection',
                    name: 'collection',
                    component: () => import('@/components/person/Collection')
                },
                {
                    path: 'focus',
                    name: 'focus2',
                    component: () => import('@/components/person/Focus')
                },
                {
                    path: 'article',
                    name: 'article',
                    component: () => import('@/components/person/Article')
                },
                {
                    path: 'idea',
                    name: 'idea',
                    component: () => import('@/components/person/Idea')
                },
                {
                    path: 'special',
                    name: 'special',
                    component: () => import('@/components/person/Special')
                },
                {
                    path: 'video',
                    name: 'video',
                    component: () => import('@/components/person/Video')
                },
                {
                    path: 'putup',
                    name: 'putup',
                    component: () => import('@/components/person/Putup')
                }
            ]
        },

    ],
},
{
    path: '/question/:id',
    name: 'question',
    component: () =>
        import('@/views/Question'),
},
{
    path: '/search/:q',
    name: 'search',
    meta: {
        needLogin: false
    },
    component: () => import('@/views/Search'),
}
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


router.beforeEach((to, from, next) => {
    // 如果退出了个人页面
    if (from.path.startsWith('/home/personal')) {
        removeSessionStorage('userListIndexActive');
    }
    if (to.name === 'active') {
        store.commit("changePersonPage", true)
    } else {
        store.commit('changePersonPage', false)
    }
    if (sessionStorage.getItem('toPath')) {
        return next()
    }
    // console.log(to);
    // 如果去的路径不是/login
    if (to.path !== '/login') {
        // 需要判断去的路由是否需要验证登录，如果需要验证登录，并且没有登录，那么就跳到登录页
        if (to.meta.needLogin && !store.state.isLogin) {
            next('/login');
            sessionStorage.setItem('toPath', to.path);
        } else {
            next();
        }
    } else {
        sessionStorage.setItem("toPath", '/');
        next();
    }
})

export default router